package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.model.Goods;
import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseVo;
import com.atguigu.gmall.list.service.ListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author gsy
 * @date 2021年09月02日23:27
 * Description
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private ListService listService;

    //创建索引库
    @PostMapping("/inner/createIndex")
    public Result createIndex(){
        //创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        //索引与实体类映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    //下架
    @DeleteMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();

    }

    //上架
    @PostMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        listService.onSale(skuId);
        return Result.ok();
    }
    //增加热度评分
    @PostMapping("/incrHotScore/{skuId}/{score}")
    public Result incrHotScore(
            @PathVariable Long skuId,
            @PathVariable Integer score){
        listService.incrHotScore(skuId,score);
        return Result.ok();

    }

    //根据查询参数对象查询商品列表
    @PostMapping
    public SearchResponseVo search(@RequestBody SearchParam searchParam){

       return listService.search(searchParam);

    }
}
